package xcmg.device.service.audit;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xcmg.device.dao.entity.abnormal.AbnormalOutDO;
import xcmg.device.dao.entity.delivery.DeliveryNoteDO;
import xcmg.device.dao.mapper.abnormal.AbnormalOutMapper;
import xcmg.device.infra.ErrorCode;
import xcmg.device.infra.enums.AbnormalOutAuditStatusEnum;
import xcmg.device.infra.enums.DeliveryNoteAuditStatusEnum;
import xcmg.device.service.crm.util.ActionResultUtil;
import xcmg.device.service.vo.delivery.BusinessOrderAuditParamVO;
import yb.ecp.fast.infra.infra.ActionResult;

import java.util.List;

@Service
public class AbnormalOutAuditService extends AbstractBusinessOrderAuditService{
    @Autowired
    private AbnormalOutMapper abnormalOutMapper;


    @Override
    public ActionResult paramBusinessCheck(BusinessOrderAuditParamVO auditParam) {
        //查询审核的数据中的状态与当前操作做对比，判断是否可以进行当前操作
        List<String> ids = auditParam.getIds();
        List<AbnormalOutDO> abnormals = abnormalOutMapper.findOrderList(ids);
        if(CollectionUtils.isEmpty(abnormals)){
            return ActionResultUtil.errorActionResult(ErrorCode.IllegalArument.getCode(),"未查询到记录");
        }
        //当勾选的数据包含审核通过、审核拒绝的数据，弹窗提示：出库单号：XXXXXX，处于审核通过状态，请重新选择！
        for(AbnormalOutDO abnormalOut : abnormals){
            Integer auditStatus = abnormalOut.getAuditStatus();
            String abnormalNo = abnormalOut.getAbnormalNo();
            if(auditStatus != null && auditStatus.compareTo(AbnormalOutAuditStatusEnum.AUDIT_PASS.getAuditStatus()) == 0){
                return ActionResultUtil.errorActionResult(ErrorCode.IllegalArument.getCode(),"出库单号："+abnormalNo+"，处于审核通过状态，请重新选择！");
            }
            if(auditStatus != null && auditStatus.compareTo(AbnormalOutAuditStatusEnum.AUDIT_REJECT.getAuditStatus()) == 0){
                return ActionResultUtil.errorActionResult(ErrorCode.IllegalArument.getCode(),"出库单号："+abnormalNo+"，处于审核驳回状态，请重新选择！");
            }
        }
        return ActionResultUtil.successActionResult();
    }

    @Override
    public void updateAuditStatus(BusinessOrderAuditParamVO auditParam,String userId,String userName,boolean pass) {
        //更新单据的审核状态
        List<String> ids = auditParam.getIds();
        String auditRemark = auditParam.getAuditRemark();
        //审核通过
        if(pass){
            abnormalOutMapper.updateAuditInfo(ids,AbnormalOutAuditStatusEnum.AUDIT_PASS.getAuditStatus(),auditRemark,userId,userName);
        }else{//审核拒绝
            abnormalOutMapper.updateAuditInfo(ids,AbnormalOutAuditStatusEnum.AUDIT_REJECT.getAuditStatus(),auditRemark,userId,userName);
        }
        return ;
    }
}
